package com.xmqian.app.project.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.coszero.common.config.Constant
import com.coszero.utils.utils.LogX
import com.coszero.utils.utils.SharedPreUtils
import com.example.xmqian.databinding.FragmentGuideBinding
import com.xmqian.app.project.common.base.BaseMyFragment
import com.xmqian.app.project.ui.user.activity.LoginActivity

/**
 * Desc： 引导界面
 *
 *
 * Author： xmqian
 * Email:xmqian93@163.com
 * Date: 2019/6/10 16:16
 *
 * @link(com.xmqian.app.project.GuideActivity)
 */
class GuideFragment : BaseMyFragment() {
    var mIvGuide: ImageView? = null

    private var resoursArray: IntArray? = null
    private var index = 0
    private var isFrist = true
    private var binding: FragmentGuideBinding? = null

    public override fun initView() {
        mIvGuide = binding!!.ivGuide
        resoursArray = requireArguments().getIntArray(RESOURS_ARRAY)
        index = requireArguments().getInt(POS, 0)
        mIvGuide!!.setImageResource(resoursArray!![index])
        if (index == 0) {
            mIvGuide!!.setImageResource(resoursArray!![index])
        }
        if (resoursArray!!.size - 1 == index) {
            mIvGuide!!.setOnClickListener { v: View? ->
                SharedPreUtils.saveInteger(
                    mContext,
                    Constant.JUMP_TYPE,
                    Constant.GUIDE_FINISH
                )
                LoginActivity.Companion.startNewTask(requireActivity())
                activity!!.finish()
            }
        }
        isFrist = false
    }

    override fun initData() {
    }

    override fun lazyLoadData() {
        if (resoursArray != null && !isFrist) {
            mIvGuide!!.setBackgroundResource(resoursArray!![index])
            LogX.i("### 引导加载第" + (index + 1) + "页")
        }
    }

    override fun getLayoutView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        attachToParent: Boolean
    ): View {
        binding = FragmentGuideBinding.inflate(inflater, container, attachToParent)
        return binding!!.root
    }

    companion object {
        const val RESOURS_ARRAY: String = "resoursArray"
        const val POS: String = "pos"
        fun newInstance(resoursArray: IntArray?, pos: Int): GuideFragment {
            val args = Bundle()
            val fragment = GuideFragment()
            args.putIntArray(RESOURS_ARRAY, resoursArray)
            args.putInt(POS, pos)
            fragment.arguments = args
            return fragment
        }
    }
}
